package com.appistry.storage;

import java.net.InetAddress;
import java.net.UnknownHostException;

import com.appistry.api.rest.domain.Credentials;
import com.appistry.api.util.Version;
import com.appistry.storage.domain.impl.StorageImpl;
import com.appistry.storage.rest.RestStorageManager;
import com.appistry.storage.rest.impl.RestStorageManagerImpl;

public class StorageManager {
	private static final int DEFAULT_HTTP_PORT = 16088;
	private static final int DEFAULT_HTTPS_PORT = 16488;
	private Storage storage;
	
	public StorageManager(String user, String password, String ip, Version version) throws UnknownHostException {
		this(user, password, ip, DEFAULT_HTTP_PORT, false, version);
	}
	
	public StorageManager(String user, String password, String ip, boolean isSSL, Version version) throws UnknownHostException {
		int port = DEFAULT_HTTP_PORT;
		
		if(isSSL){
			port = DEFAULT_HTTPS_PORT;
		}
		
		InetAddress address = InetAddress.getByName(ip);
		Credentials credentials = new Credentials(user, password);
		RestStorageManager manager = new RestStorageManagerImpl(address, port, isSSL, credentials, version);
		this.storage = new StorageImpl(manager);
	}
	
	public StorageManager(String user, String password, String ip, int port, Version version) throws UnknownHostException {
		this(user, password, ip, port, false, version);
	}	
	
	public StorageManager(String user, String password, String ip, int port, boolean isSSL, Version version) throws UnknownHostException {
		InetAddress address = InetAddress.getByName(ip);
		Credentials credentials = new Credentials(user, password);
		RestStorageManager manager = new RestStorageManagerImpl(address, port, isSSL, credentials, version);
		this.storage = new StorageImpl(manager);
	}
	
	public StorageManager(Credentials credentials, InetAddress address, int port, boolean isSSL, Version version) {
		RestStorageManager manager = new RestStorageManagerImpl(address, port, isSSL, credentials, version);
		this.storage = new StorageImpl(manager);
	}

	public Storage getStorage(){
		return storage;
	}
}
